import { t } from 'i18next'
import { useCreateContentStore } from '../../store'
import { useBasicInfoStore } from '../basicInfo/store'
import { resetBitrate } from '../user/store.utils'
import { useMeetingParamsStore } from './store'

/** 恢复媒体能力默认值 */
export function resetBitrateType() {
	const permission = useCreateContentStore.getState().permission
	const { meetingType } = useMeetingParamsStore.getState()
	let newVal = meetingType === 2 ? (permission.enableHDMeeting ? 6 : 4) : 3
	useMeetingParamsStore.setState({ _bitrateType: newVal })
	changeBitrateType(newVal)
}

export function changeBitrateType(val: number) {
	const meetingType = useMeetingParamsStore.getState().meetingType
	if (val !== 0) {
		if (meetingType === 2) {
			setSfuMediaValue(val)
		} else {
			setMediaAbilityVal(val)
		}
	}
}

/** 设置自定义媒体能力下拉框值*/
export const setSelfMediaAbilityVal = (val: number) => {
	const { update, _H265, meetingType } = useMeetingParamsStore.getState()
	update({ _selfMediaValue: val })
	switch (val) {
		case 1:
			// CIF@25fps
			update({
				resolution: 2,
				frame: 25,
			})
			break
		case 2:
			// WCIF@25fps
			update({
				resolution: 14,
				frame: 25,
			})
			break
		case 3:
			// 4CIF@25fps
			update({
				resolution: 3,
				frame: 25,
			})
			break
		case 4:
			// W4CIF@25fps
			update({
				resolution: 15,
				frame: 25,
			})
			break
		case 5:
			// 720@30
			update({
				resolution: 12,
				frame: 30,
			})
			break
		case 6:
			// 1080@30
			update({
				resolution: 13,
				frame: 30,
			})
			break
		case 7:
			// 720@60
			update({
				resolution: 12,
				frame: 60,
			})
			break
		case 8:
			// 1080@60
			update({
				resolution: 13,
				frame: 60,
			})
			break
		case 9:
			//4K@30
			update({
				resolution: 16,
				frame: 30,
			})
			break
		case 10:
			//4K@60
			update({
				resolution: 16,
				frame: 60,
			})
			//当传统会议勾选h265和4K60后，默认下参纯转发
			if (_H265 && meetingType === 0) {
				update({ sourceUsed: 2 })
			}
			break
		case 0:
			//无
			update({
				resolution: 0,
				frame: 0,
			})
			break
		default:
	}
}

/** 获取自定义媒体能力下拉框值 */
export const getSelfMediaAbilityVal = (resolution: number, frame: number) => {
	if (resolution === 2 && frame === 25) {
		return 1
	} else if (resolution === 14 && frame === 25) {
		return 2
	} else if (resolution === 3 && frame === 25) {
		return 3
	} else if (resolution === 15 && frame === 25) {
		return 4
	} else if (resolution === 12 && frame === 30) {
		return 5
	} else if (resolution === 13 && frame === 30) {
		return 6
	} else if (resolution === 12 && frame === 60) {
		return 7
	} else if (resolution === 13 && frame === 60) {
		return 8
	} else if (resolution === 16 && frame === 30) {
		return 9
	} else if (resolution === 16 && frame === 60) {
		return 10
	}
	return 0
}

/** 设置sfu媒体能力值 */
export const setSfuMediaValue = (val: number) => {
	const update = useMeetingParamsStore.getState().update
	const _preRate = useMeetingParamsStore.getState()._preRate
	update({ _bitrateType: val })
	if (val === 4) {
		update({
			frame: 30,
			rate: 2048,
			resolution: 12,
			video: 4,
		})
		resetBitrate(_preRate, 2048)
	} else {
		update({
			frame: 30,
			rate: 4096,
			resolution: 13,
			video: 4,
		})
		resetBitrate(_preRate, 4096)
	}
}

/** 媒体能力下拉框值改变 */
export const setMediaAbilityVal = (val: number) => {
	const { update, _H265, _preRate, meetingType } = useMeetingParamsStore.getState()
	update({ _bitrateType: val })
	let video = _H265 ? 6 : 4
	switch (val) {
		case 0:
			update({
				frame: 0,
				rate: undefined,
				resolution: 0,
				video: video,
			})
			break
		case 1:
			// WCIF@25fps 192K
			update({
				frame: 25,
				rate: 192,
				resolution: 14,
				video: video,
			})
			break
		case 2:
			// 标清（W4CIF@25fps 256K）
			update({
				frame: 25,
				rate: 256,
				resolution: 15,
				video: video,
			})
			break
		case 3:
			// 高清（720P@30 2M）
			update({
				frame: 30,
				rate: 2048,
				resolution: 12,
				video: video,
			})
			break
		case 4:
			// 全高清（1080P@30 2M）
			update({
				frame: 30,
				rate: 2048,
				resolution: 13,
				video: video,
			})
			break
		case 5:
			// 全高清（1080P@60 4M）
			update({
				frame: 60,
				rate: 4096,
				resolution: 13,
				video: video,
			})
			break
		case 6:
			// 超高清（4K@30 4M）
			update({
				frame: 30,
				rate: 4096,
				resolution: 16,
				video: video,
			})
			break
		case 7:
			// 超高清（4K@60 8M）
			update({
				frame: 60,
				rate: 8192,
				resolution: 16,
				video: video,
			})
			//当传统会议勾选h265和4K60后，默认下参纯转发
			if (_H265 && meetingType === 0) {
				update({ sourceUsed: 2 })
			}
			break
	}
	let curRate = useMeetingParamsStore.getState().rate
	resetBitrate(_preRate, curRate)
}

/** 设置自定义媒体能力下拉框列表 */
export const setSelfMediaAbilityList = (val: number | undefined) => {
	const permission = useCreateContentStore.getState().permission
	const superMtNum = useBasicInfoStore.getState().superMtNum
	const { update, _H265, meetingType, enableRtc } = useMeetingParamsStore.getState()
	const isPortMeeting = meetingType === 1 && !enableRtc

	val = val ?? 0
	let list = []
	if (_H265) {
		list = getH265SupportFpsByBiterate(val, isPortMeeting)
		if (!permission.has60FpsInH265) {
			list = list.filter(function (value) {
				return !/60fps/i.test(value.label)
			})
		}
	} else {
		list = getH264SupportFpsByBiterate(val, isPortMeeting)
	}
	if (!permission['60fps']) {
		list = list.filter(function (value) {
			return !/60fps/i.test(value.label)
		})
	}
	if (!permission['4K']) {
		list = list.filter(function (value) {
			return !/4K/i.test(value.label)
		})
	}
	if (!permission.enableHDMeeting) {
		list = list.filter(function (value) {
			return !(/1080P/i.test(value.label) || /4K/i.test(value.label))
		})
	}
	//只有纯转发会议才能有4K@60fps
	//超大方开启与传统会议4k@60互斥
	if (meetingType !== 0 || superMtNum) {
		list = list.filter(function (value) {
			return !/4K@60fps/i.test(value.label)
		})
	}

	update({ _selfMediaList: list })
}

/** H265勾选下根据自定义媒体能力下拉框列表*/
export const getH265SupportFpsByBiterate = (bite: number, isPortMeeting: boolean) => {
	let supportedFps = []
	if (bite <= 64 || !bite) {
		supportedFps.unshift({
			value: 0,
			label: t('无'),
		})
		return supportedFps
	}
	if (bite > 64) {
		supportedFps.unshift({
			value: 1,
			label: 'CIF@25fps',
		})
	}
	if (bite > 128) {
		supportedFps.unshift({
			value: 2,
			label: 'WCIF@25fps',
		})
	}
	if (bite > 192) {
		supportedFps.unshift(
			{
				value: 4,
				label: 'W4CIF@25fps',
			},
			{
				value: 3,
				label: '4CIF@25fps',
			}
		)
	}
	if (bite > 320) {
		supportedFps.unshift({
			value: 5,
			label: '720P@30fps',
		})
	}
	if (bite > 448) {
		supportedFps.unshift({
			value: 7,
			label: '720P@60fps',
		})
	}
	if (isPortMeeting) {
		//端口会议调整低码率高清
		if (bite > 320) {
			supportedFps.unshift({
				value: 6,
				label: '1080P@30fps',
			})
		}
		if (bite > 704) {
			supportedFps.unshift({
				value: 8,
				label: '1080P@60fps',
			})
		}
	} else {
		if (bite > 448) {
			supportedFps.unshift({
				value: 6,
				label: '1080P@30fps',
			})
		}
		if (bite > 960) {
			supportedFps.unshift({
				value: 8,
				label: '1080P@60fps',
			})
		}
	}
	if (bite > 1472) {
		supportedFps.unshift(
			{
				value: 10,
				label: '4K@60fps',
			},
			{
				value: 9,
				label: '4K@30fps',
			}
		)
	}
	return supportedFps
}
/** H264勾选下根据自定义媒体能力下拉框列表*/
export const getH264SupportFpsByBiterate = (bite: number, isPortMeeting: boolean) => {
	let supportedFps = []
	if (bite <= 64 || !bite) {
		supportedFps.unshift({
			value: 0,
			label: t('无'),
		})
		return supportedFps
	}

	if (bite > 64) {
		supportedFps.unshift({
			value: 1,
			label: 'CIF@25fps',
		})
		supportedFps.unshift({
			value: 2,
			label: 'WCIF@25fps',
		})
	}
	if (bite > 128) {
		supportedFps.unshift({
			value: 3,
			label: '4CIF@25fps',
		})
	}
	if (bite > 192) {
		supportedFps.unshift({
			value: 4,
			label: 'W4CIF@25fps',
		})
	}
	if (bite > 320) {
		supportedFps.unshift({
			value: 5,
			label: '720P@30fps',
		})
	}
	if (bite > 704) {
		supportedFps.unshift({
			value: 7,
			label: '720P@60fps',
		})
	}
	if (isPortMeeting) {
		if (bite > 448) {
			supportedFps.unshift({
				value: 6,
				label: '1080P@30fps',
			})
		}
		if (bite > 960) {
			supportedFps.unshift({
				value: 8,
				label: '1080P@60fps',
			})
		}
	} else {
		if (bite > 704) {
			supportedFps.unshift({
				value: 6,
				label: '1080P@30fps',
			})
		}
		if (bite > 1984) {
			supportedFps.unshift({
				value: 8,
				label: '1080P@60fps',
			})
		}
	}

	return supportedFps
}

/** 判断自定义媒体能力值是否存在于下拉列表,不存在则选项选择最顶上的选项 */
export const checkSelfMediaValueInList = () => {
	const { _selfMediaList, _selfMediaValue, update } = useMeetingParamsStore.getState()
	if (_selfMediaList.map((item) => item.value).indexOf(_selfMediaValue) === -1) {
		//该值不存在于下拉列表,选项选择最顶上的选项
		update({ _selfMediaValue: Number(_selfMediaList[0].value) })
	}
}
